package cc.magicjson.spring.exploration.advisor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

@Component // 将这个类标记为Spring组件
public class InfrastructureAdvice implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // 检查被调用的方法是否属于非Spring框架类
        if (invocation.getThis() != null && !invocation.getThis().getClass().getName().startsWith("org.springframework")) {
            // 在方法执行前打印日志
            System.out.println("Infrastructure advice - Before method execution: " + invocation.getMethod().getName());
            // 执行原方法
            Object result = invocation.proceed();
            // 在方法执行后打印日志
            System.out.println("Infrastructure advice - After method execution: " + invocation.getMethod().getName());
            return result;
        }
        // 如果是Spring框架的类，直接执行原方法而不进行拦截
        return invocation.proceed();
    }
}
